<?php
namespace Admin\Controller;
use Think\Controller;
require_once 'Qiniu/autoload.php';
use \Qiniu\Auth;
use \Qiniu\Storage\UploadManager;
use Admin\Controller\CommonController;

/**
 * 启动图管理
 * @package Admin\Controller
 */
class LaunchController extends CommonController {

    public function index() {
        $putPolicy=array(
            'saveKey'=>'launch/$(etag)',
        );
        $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
        //$bucket = 'images';
        $bucket = C('QINIU_BUCKET');
        $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);


        $this->assign('uploadToken', $uploadToken);

        $v_model = M('FrontLaunch');

        $total = $v_model->where($map)->count();
        $page = new \Think\Page($total, 100);
        $show = $page->show();

        $vList = $v_model->where($map)
            ->limit($page->firstRow .',' . $page->listRows)
            ->order('weight desc,create_time DESC,id desc')
            ->select();


        $this->assign('showpage', $show);
        $this->assign('vList', $vList);
        $this->display();
    }

    public function add() {
        if(IS_POST) {
            $model = D('FrontLaunch');

            $_POST['expire_date'] = strtotime(I('post.expire_date')." 23:59:59");

            if($model->create()) {
                $ret = $model->add();
                if($ret) {
                    /*if($_POST['is_use']) {
                        D('FrontLaunch')->where('id <> ' . $ret)->save(array('is_use' => 0));
                    }*/

                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }

            } else {
                $this->error($model->getError());
            }
        }
    }


    public function edit() {
        if(IS_POST) {
            $model = D('FrontLaunch');

            $_POST['expire_date'] = strtotime(I('post.expire_date')." 23:59:59");

            if($model->create()) {
                $ret = $model->save();
                if($ret===false) {
                    $this->error('修改失败');

                } else {
                    $this->success('修改成功');
                }

            } else {
                $this->error($model->getError());
            }
        }
    }

    public function ajaxdellaunch($id){
        $model=M('FrontLaunch');
        if($id){
            $map['id'] = $id;
            $ret = $model->where($map)->delete();
            if($ret){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            return false;
        }
    }


    public function ajaxGetLaunchInfo($id) {
        $model =M('FrontLaunch');
        if($id){
            $info=$model->where('id='.$id)->find();
            if($info){
                $info['expire_date']=date("Y-m-d",$info['expire_date']);
                if($info['type']==1){
                    $info['links']=json_encode(unserialize(html_entity_decode($info['links'])));
                }

                $this->ajaxReturn($info);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            return false;
        }
    }


    public function validImg(){
        $upload = new \Think\Upload();
        $upload->rootPath  =    '/Uploads/';
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath  =      'validImg/'; // 设置附件上传目录
        $upload->saveName  =      time().'_'.mt_rand();
        $upload->subName   =      array('date','Y/m/d');
        $upload->autosub   =      true;

        $info   =   $upload->upload();
        if(!$info) {
            $mess['resultCode']=1;
            $mess['info']=$upload->getError();
        }else{
            $mess['resultCode']=0;
            $mess['info']=$info['file']['savepath'].$info['file']['savename'];
        }

        $this->ajaxReturn($mess);
    }
}